#!/bin/bash -e

function usage
	{
	echo -e "usage: drupalbackups [-db database][[[-h][-hour]][[-d][-day]][[-w][-week]][[-m][-month]]][[[-u][-user] username][[-p][-pass] password]][-b][--backup] /path/to/folder/]\n --h, --help for more detailed instructions"
	}

function help
	{
	echo -e "Frequency(required):"
  	echo -e "	-h:--hour:  Perform an hourly backup and"
	echo -e "		     delete anything older than 48 hours"
  	echo -e "	-d:--day:   Perform a daily backup and"
  	echo -e "		     delete anything older than 14 days"
  	echo -e "	-w:--week:  Perform a weekly backup and"
  	echo -e "		     delete anything older than 1 month"
  	echo -e "	-m:--month: Perform a monthly backup and"
  	echo -e "		     delete anything older than 1 year"
  	echo -e "Database:"
  	echo -e "	-db:--database (dbname): required unless specified"
  	echo -e "			         in my.cnf"
  	echo -e "Server:"
  	echo -e "	-s:--server (server):    if not specified default is"
  	echo -e "                                in my.cnf else will defualt to localhost"
  	echo -e "Login:"
  	echo -e "	-u:--user (username):    required unless specified in my.cnf"
  	echo -e "	-p:--pass (password):    required unless specified in my.cnf"
  	echo -e "Backup path:"
  	echo -e "	-b:--backup (/path/to/folder/)   required unless specified"
  	echo -e "                                        in .drupalbackup"
	}



#function to dumpdata and skip cache files

function dump
	{
	query=''
	tables=$(mysql $db $user $pass -B --column-names=False -e "SHOW TABLES where tables_in_$db not like 'cache_%'")
	#echo $tables
	mysqldump $db $user $pass $tables > /tmp/$db\-$time\-$now.sql
	}
function compress
	{

	tar czfP $backup_path$db\-$time\-$now.sql.tgz /tmp/$db\-$time\-$now.sql

	}

function delete
	{
	if [[ -n $time ]];then
	{
	find $backup_path -type f -name "$db-$time*sql.tgz" -mtime +$fromtoday -exec rm {} \;
	rm /tmp/$db*.sql
	}
	fi
	}



fromtoday=	
yellow="\e[01;33m"
endcolor="\e[00m"	
time=
user=
pass=
now=$(date +%Y-%m-%d-%H-%M)


while [ "$1" != "" ]; do
    case $1 in
        -h | --hour )           time="hourly"	
                                ;;
        -d | --day )            time="daily"
                                ;;
        -w | --week )           time="weekly"
                                ;;
        -m | --month )          time="monthly"
                                ;;
        -db | --database )      shift
				db="$1"
                                ;;
        -u | --user )	        shift
				user="-u $1"
                                ;;
        -p | --pass )      	shift
				pass="--password=$1"
                                ;;
        -s | --server )      	shift
				server="-h $1"
                                ;;
        -b | --backup )      	shift
				backup_path="$1"
                                ;;
        --h | --help )          help
                                exit
                                ;;
        * )                     usage
                                exit
    esac
    shift
done

case $time in
       hourly )          	fromtoday="2"	
                                ;;
       daily )          	fromtoday="14"	
                                ;;
       weekly )          	fromtoday="32"	
                                ;;
       monthly )          	fromtoday="365"	
                                ;;
        * )                     echo -e $yellow"Time not set performing generic database dump"$endcolor
esac
if [[ -a "$HOME/.drupalbackup" && -z $backup_path ]]; then
{
. $HOME/.drupalbackup
}
fi

if [[ -a "$HOME/.my.cnf" && -z $db ]]; then
{
db=$(grep "database*" $HOME/.my.cnf|awk '{ print  $3 }')

}
fi

if [[ -a "$HOME/.my.cnf" && -z $user && -n $(grep "user*" $HOME/.my.cnf|awk '{ print  $3 }') ]]; then
{
user="-u $(grep 'user*' $HOME/.my.cnf|awk '{ print  $3 }')"
}
fi
if [[ -a "$HOME/.my.cnf" && -z $pass && -n $(grep "password*" $HOME/.my.cnf|awk '{ print  $3 }') ]]; then
{
pass="--password=$(grep 'password*' $HOME/.my.cnf|awk '{ print  $3 }')"
}
fi

if [[ -z $db ]];then
{
echo -e $yellow"Database not set, please pass the variable with -db or set it in ~/.my.cnf"$endcolor
exit 0
}
fi
if [[ -z $user ]];then
{
echo -e $yellow"User not set, please pass the variable with -u or set it in ~/.my.cnf"$endcolor
exit 0
}
fi
if [[ -z $pass ]];then
{
echo -e $yellow"Password not set, please pass the variable with -p or set it in ~/.my.cnf"$endcolor
exit 0
}
fi
if [[ -z $server ]];then
{
echo -e $yellow"Server not set, assuming localhost"$endcolor
server="-h localhost"
}
fi
if [ -z $backup_path ]; then
	echo -e $yellow"The backup_path is not set with variable or in ~.drupalbackup, assuming current directory"$endcolor
fi 


dump
compress
delete
exit 0

